Attribute VB_Name = "EditorDeRegistro"
 Public Declare Function RegCloseKey Lib "advapi32" _
   (ByVal hKey As Long) As Long

Public Declare Function RegCreateKey Lib "advapi32" _
    Alias "RegCreateKeyA" (ByVal hKey As Long, _
    ByVal lpSubKey As String, phkResult As Long) As Long

Public Declare Function RegDeleteValue Lib "advapi32" _
    Alias "RegDeleteValueA" (ByVal hKey As Long, _
    ByVal lpValueName As String) As Long

Public Declare Function RegOpenKeyEx Lib "advapi32" _
    Alias "RegOpenKeyExA" (ByVal hKey As Long, _
    ByVal lpSubKey As String, ByVal ulOptions As Long, _
    ByVal samDesired As Long, phkResult As Long) As Long

Public Declare Function RegQueryValueEx Lib "advapi32" _
    Alias "RegQueryValueExA" (ByVal hKey As Long, _
    ByVal lpValueName As String, ByVal lpReserved As Long, _
    lpType As Long, lpData As Any, lpcbData As Long) As Long

Public Declare Function RegSetValueEx Lib "advapi32" _
    Alias "RegSetValueExA" (ByVal hKey As Long, _
    ByVal lpValueName As String, ByVal Reserved As Long, _
    ByVal dwType As Long, lpData As Any, _
    ByVal cbData As Long) As Long

Public Const REG_NONE = 0
Public Const REG_SZ = 1
Public Const REG_EXPAND_SZ = 2
Public Const REG_BINARY = 3
Public Const REG_DWORD = 4
Public Const REG_DWORD_LITTLE_ENDIAN = 4
Public Const REG_DWORD_BIG_ENDIAN = 5
Public Const REG_LINK = 6
Public Const REG_MULTI_SZ = 7
Public Const REG_RESOURCE_LIST = 8

Public Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Private Function GetSubkeyHandle(sSubkey As String) As Long

    Dim intSubkeyCount As Integer
    Dim intSubkeyLoop As Integer
    Dim intSubkeyPtr As Integer
    Dim lonHandle(1 To 32) As Long
    Dim lonLastHandle As Long
    Dim lonStatus As Long
    Dim strKeyPath As String
    Dim strSubkeys(1 To 32) As String
    Dim strTempKeys() As String
    
    ' Trim any spaces from left and right sides of the
    ' subkey path.
    strKeyPath = Trim$(sSubkey)
    
    ' Use VB's Split function to break the key's path
    ' into subkeys.
    
    strTempKeys = Split(strKeyPath, "\")
    
    ' Transfer the elements of the strTempKeys()
    ' array to strSubkeys(), but leave out any blank
    ' elements.
    
    For intSubkeyLoop = 0 To UBound(strTempKeys)
        If strTempKeys(intSubkeyLoop) <> "" Then
            intSubkeyCount = intSubkeyCount + 1
            If intSubkeyCount <= 32 Then
                strSubkeys(intSubkeyCount) = strTempKeys(intSubkeyLoop)
            Else
                ' Error - too many subkeys in path.
                GetSubkeyHandle = -1
                Exit Function
            End If
        End If
    Next intSubkeyLoop
    
    For intSubkeyPtr = 1 To intSubkeyCount - 1
        ' For the first subkey, use the defined
        ' constants - this is the root key.
        If intSubkeyPtr = 1 Then
            Select Case UCase$(strSubkeys(1))
                Case "HKEY_CLASSES_ROOT":
                    lonLastHandle = &H80000000
                Case "HKEY_CURRENT_USER":
                    lonLastHandle = &H80000001
                Case "HKEY_LOCAL_MACHINE"
                    lonLastHandle = &H80000002
                Case "HKEY_USERS":
                    lonLastHandle = &H80000003
                Case "HKEY_PERFORMANCE_DATA":
                    lonLastHandle = &H80000004
                Case "HKEY_CURRENT_CONFIG":
                    lonLastHandle = &H80000005
                Case Else:
                    GetSubkeyHandle = -1
                    Exit Function
            End Select
        End If
        ' Open the next subkey.
        lonStatus = RegOpenKeyEx(lonLastHandle, _
            strSubkeys(intSubkeyPtr + 1), 0, &H3F, _
           lonHandle(intSubkeyPtr))
        ' A non-zero value returned by the
        ' RegOpenKey function indicates an error.
    
        If lonStatus <> 0 Then ' SACAR NI NO ES ASI "<> 0"
            ' Error in path. Close any previously
            ' opened subkeys.
            For intSubkeyLoop = 2 To (intSubkeyPtr - 1)
                lonStatus = RegCloseKey(lonHandle(intSubkeyLoop))
            Next intSubkeyLoop
            GetSubkeyHandle = -1
            Exit Function
        End If
    
        lonLastHandle = lonHandle(intSubkeyPtr)
    Next intSubkeyPtr
    
    ' Return the value of the last subkey handle.
    GetSubkeyHandle = lonLastHandle
    
    ' Close all subkeys but the last one.
    For intSubkeyLoop = 1 To (intSubkeyCount - 2)
        lonStatus = RegCloseKey(lonHandle(intSubkeyLoop))
    Next intSubkeyLoop

End Function


Public Function RegDelete(SubKeyPath As String, Key As String) As String

    Dim lonSubkeyHandle As Long
    Dim lonStatus As Long
    
    ' Use the GetSubkeyHandle function to determine the
    ' handle of the subkey given in txtSectionName.
    
    lonSubkeyHandle = GetSubkeyHandle(SubKeyPath)
    
    If lonSubkeyHandle = -1 Then
        ' Subkey could not be located.
        RegDelete = "Error - Subkey could not be located"
        Exit Function
    End If
    
    ' Use the RegDeleteKey API function to delete the
    ' key.
    
    lonStatus = RegDeleteValue(lonSubkeyHandle, Key)
    
    ' Check status of the operation.
    
    If lonStatus <> 0 Then
        RegDelete = "Error - Key could not be deleted"
    Else
        RegDelete = "Key deleted sucessfully"
    End If
    
    ' Close the open subkey with the CloseSubKey
    ' function.
    
    lonStatus = CloseSubkey(lonSubkeyHandle)
    
End Function

Private Function CloseSubkey(lHandle As Long) As Long

    Dim lonStatus As Long
    
    ' Close the key with the handle specified by the
    ' lHandle argument.
    
    lonStatus = RegCloseKey(lHandle)
    ' Return zero if successful, -1 if not successful.
    
    CloseSubkey = 0
    If lonStatus Then CloseSubkey = -1
    
End Function

Public Function RegAdd(SubKeyPath As String, Key As String, Value As String) As String

    Dim lonSubkeyHandle As Long
    Dim lonStatus As Long
    
    ' Use the GetSubkeyHandle function to determine the
    ' handle of the subkey given in txtSectionName.
    
    lonSubkeyHandle = GetSubkeyHandle(SubKeyPath)
    
    If lonSubkeyHandle = -1 Then
        ' Subkey could not be located.
        RegAdd = "Error - Subkey could not be located"
        Exit Function
    End If
    
    ' Use the RegSetValueEx API function to create a
    ' new key and set its value.
    
    lonStatus = RegSetValueEx(lonSubkeyHandle, _
        Key, 0, REG_SZ, ByVal Value, _
        Len(Value))
    
    ' Check status of the operation.
    If lonStatus <> 0 Then
        RegAdd = "Error - Key could not be added"
    Else
        RegAdd = "Key added sucessfully"
    End If
    
    ' Close the open subkey with the CloseSubKey
    ' function.
    lonStatus = CloseSubkey(lonSubkeyHandle)

End Function

Public Function RegUpdate(SubKeyPath As String, Key As String, Value As String) As String

    Dim lonSubkeyHandle As Long
    
    Dim lonStatus As Long
    
    ' Use the GetSubkeyHandle function to determine the
    ' handle of the subkey given in SubKeyPath.
    
    lonSubkeyHandle = GetSubkeyHandle(SubKeyPath)
    
    If lonSubkeyHandle = -1 Then
        ' Subkey could not be located.
        RegUpdate = "Error - Subkey could not be located"
        Exit Function
    End If
    
    ' Use the RegSetValueEx API function to update the
    ' key's value.
    
    lonStatus = RegSetValueEx(lonSubkeyHandle, _
        Key, 0, REG_SZ, ByVal Value, _
        Len(Value))
    
    ' Check status of the operation.
    
    If lonStatus <> 0 Then
        RegUpdate = "Error - Key could not be updated"
    Else
        RegUpdate = "Key updated sucessfully"
    End If
    
    ' Close the open subkey with the CloseSubKey
    ' function.
    
    lonStatus = CloseSubkey(lonSubkeyHandle)
    
End Function

Public Function RegRetrieve(SubKeyPath As String, Key As String) As String

    Dim lonSubkeyHandle As Long
    Dim lonStatus As Long
    Dim lonValueType As Long
    Dim lonValueSize As Long
    
    ' Use the GetSubkeyHandle function to determine the
    ' handle of the subkey given in txtSectionName.
    
    lonSubkeyHandle = GetSubkeyHandle(SubKeyPath)
    
    If lonSubkeyHandle = -1 Then
        ' Subkey could not be located.
        RegRetrieve = "Error - Key could not be located"
        Exit Function
    End If
    
    ' Use the RegQueryValueEx API function to retrieve
    ' the value of the key given in txtKeyName.
    
    lonValueType = REG_SZ
    lonValueSize = 256
    
    
    Dim gstrKeyValue As String * 256
    
    lonStatus = RegQueryValueEx(lonSubkeyHandle, _
       Key, 0, lonValueType, _
        ByVal gstrKeyValue, lonValueSize)
    
    
    If lonStatus <> 0 Then
        ' Key could not be located.
        RegRetrieve = "Error - Key could not be located"
    Else
    ''    RegRetrieve = "Key retrieved successfully"
        If InStr(1, gstrKeyValue, Chr(0)) <> 0 Then
            lonValueSize = lonValueSize - 1
        End If
        RegRetrieve = Left$(gstrKeyValue, lonValueSize)
    End If
    
    ' Close the open subkey with the CloseSubKey
    ' function.
    lonStatus = CloseSubkey(lonSubkeyHandle)
    
End Function


